const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

// 水果-仓库关联表（多对多关系）
const FruitWarehouse = sequelize.define('FruitWarehouse', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  fruit_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '水果ID',
    references: {
      model: 'fruits',
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  },
  warehouse_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '仓库ID',
    references: {
      model: 'warehouses',
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '该仓库中此水果的数量'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    field: 'created_at'
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    field: 'updated_at'
  }
}, {
  tableName: 'fruit_warehouses',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      unique: true,
      fields: ['fruit_id', 'warehouse_id']
    }
  ]
});

module.exports = FruitWarehouse;

